#!/bin/bash

CATALINA_BASE="/var/lib/tomcat6"
PROPS="$CATALINA_BASE/shared/classes/alfresco-global.properties"

# Get the public host name and any user data
ec2_public_hostname=`curl http://169.254.169.254/latest/meta-data/public-hostname -s`
user_data=`curl http://169.254.169.254/latest/user-data -s`

test -n "$ec2_public_hostname" && sed -i "s/hostname\\.public=.*/hostname.public=$ec2_public_hostname/" "$PROPS"

# Add properties specified in the user data to alfresco-global.properties
if [[ ! "$user_data" == \<* ]]; then
  awk '{ if (inblock!=1) print $0} /^# START USER DATA/ {inblock=1}' "$PROPS" > "$PROPS.new"
  echo "$user_data" >> "$PROPS.new"
  mv "$PROPS.new" "$PROPS"
fi
